package com.xbn.xbnmall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author XBN
 * @date 2020/8/17 22:24
 * @email 78742541@qq.com
 */
@Configuration
public class XbnFeignConfig {
    /*
    feign的拦截器
    */
        @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
            return new RequestInterceptor() {
                @Override
                public void apply(RequestTemplate requestTemplate) {
              ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                    System.out.println("查询用户地址线程号："+Thread.currentThread().getId());
                    if(servletRequestAttributes!=null)
                    {
                        HttpServletRequest request = servletRequestAttributes.getRequest();
                        //同步请求头数据
                        requestTemplate.header("Cookie",request.getHeader("Cookie"));
                    }

                }
            };
        }
}
